<?php

namespace App;

use App\Library\PicCompress;
use Illuminate\Database\Eloquent\Model;
use DB;
use Storage;
use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Core\OssUtil;

/**
 * Class Postings
 * @package App
 */
class Postings extends Model
{
    public $timestamps = false;
    protected $table = 'tbl_archive';

    //是否可以回复
    const REPLY_ON = 1;
    const REPLY_OFF = 2;

    //图片还是视频
    const PORM_PIC = 1;
    const PORM_VIDEO = 2;


    //是否展示
    const VIEW_NO = 0;
    const VIEW_YES = 1;
    static $ViewDisplay = [
        self::VIEW_NO => '隐藏',
        self::VIEW_YES => '展示',
    ];

    //是否加精
    const HOT_YES = 1;
    const HOT_NO = 0;
    static $HotDisplay = [
        self::HOT_YES => '精品',
        self::HOT_NO => '非精品',
    ];

    //每页显示的回帖数量
    const POSTINGS_HUI = 10;

    //帖子类型
    static $TypeDisplay = [
        '健康养生',
        '生活秘方',
        '热门求助',
        '疾病分享',
        '两性',
        '妇产科',
        '儿科',
        '中医科',
        '内科',
        '外科',
        '皮肤科',
        '口腔科',
        '骨科',
        '耳鼻喉科',
        '眼科',
        '肿瘤科',
        '呼吸内科',
        '消化内科',
        '神经内科',
        '精神科',
    ];

    //发帖城市
    static $CityDisplay = [
        '保定'
    ];

    /**
     * 帖子详情
     * @param $post_id
     * @return array
     */
    static function PostingsInfo($post_id)
    {
        $postings = DB::table('tbl_archive')->select('archive_id', 'ctype', 'member_id', 'create_time', 'content', 'ping_num', 'can_reply')->where('archive_id', $post_id)->first();
        if (!$postings) {
            return ['status' => 2, 'message' => '无此帖子'];
        }
        $pasges = round($postings->ping_num / self::POSTINGS_HUI);
        $return = [
            'archive_id' => $postings->archive_id,
            'ctype' => $postings->ctype,
            'member_id' => $postings->member_id,
            'name' => DB::table('t_member')->where('c_id', $postings->member_id)->value('name'),
            'create_time' => $postings->create_time,
            'content' => $postings->content,
            'ping_num' => $postings->ping_num,
            'can_reply' => $postings->can_reply,
            'pages' => $pasges
        ];
        return ['status' => 1, 'message' => $return];
    }


    /**
     * 返回帖子的回复
     * @param $article_id
     * @param $page
     * @return array
     */
    static function PostingsDetails($article_id, $page)
    {
        $returns = [];
        $details = DB::table('tbl_article_comment')
            ->select('id', 'member_id', 'content', 'create_time', 'good_num')
            ->where('article_id', $article_id)
            ->orderBy('id')
            ->offset(($page - 1) * self::POSTINGS_HUI)
            ->limit(self::POSTINGS_HUI)
            ->get();

        if ($details) {
            foreach ($details as $detail) {
                //用户信息
                $userinfo = DB::table('t_member')->select('name', 'c_avatar')->where('c_id', $detail->member_id)->first();
                //楼层回复
                $replys = DB::table('tbl_archive_reply')
                    ->select('id', 'user_id', 'user_name', 'reuser_id', 'reuser_name', 'content', 'create_time')
                    ->where('lou_id', $detail->id)
                    ->orderBy('id')
                    ->get();
                $returns[] = [
                    'member_id' => $detail->member_id,
                    'id' => $detail->id,
                    'name' => $userinfo->name,
                    'c_avatar' => $userinfo->c_avatar,
                    'content' => $detail->content,
                    'create_time' => $detail->create_time,
                    'good_num' => $detail->good_num,
                    'replys' => $replys,
                ];
            }
        }
        return $returns;
    }


    /**
     * 发帖
     * @param $request
     * @return array
     * @throws \Exception
     */
    static function Post($request)
    {
        $time = date('Y-m-d H:i:s', time());
        DB::beginTransaction();
        //添加behavior_log
        $behavior_log = BehaviorLog::UserBehaviorLog($request->user_id, 'post_times');
        if ($behavior_log['status'] !== 1) {
            DB::rollBack();

            return $behavior_log;
        }
        //判断view_time和hot_time
        $is_view = self::VIEW_NO;
        $is_hot = self::HOT_NO;
        $hot_time = null;
        $pub_time = $request->view_time;
        if ($request->filled('view_time') && intval($request->view_time) === 1) {
            $is_view = self::VIEW_YES;
            $pub_time = $time;
        }
        if ($request->filled('hot_time')) {
            if (intval($request->hot_time) === 1) {
                $is_hot = self::HOT_YES;
                $hot_time = $time;
            } else {
                $hot_time = $request->hot_time;
            }

        }
        //数据存表
        $post_id = DB::table('tbl_archive')->insertGetId([
            'ctype' => '生活',
            'city' => '保定',
            'member_id' => $request->user_id,
            'ping_num' => 0,
            'good_num' => 0,
            'bad_num' => 0,
            'view_rank' => 0,
            'view_count' => 0,
            'keshi_id' => $request->keshi_id,
            'keshi' => Keshi::where('id', $request->keshi_id)->value('title'),
            'title' => $request->title,
            'huati_id' => $request->huati_id,
            'huati' => Huati::where('id', $request->huati_id)->value('title'),
            'is_view' => $is_view,
            'view_time' => $request->view_time,
            'is_hot' => $is_hot,
            'hot_time' => $hot_time,
            'content' => strip_tags($request->contents),
            'pub_time' => $pub_time
        ]);
        if (!$post_id) {
            DB::rollBack();

            return ['status' => 2, 'message' => '发帖失败'];
        }
        if ($request->has('file1')) {
            //上传视频或者图片
            if (intval($request->porm) === self::PORM_PIC) {
                $picInsert = [];
                for ($i = 1; $i <= 9; $i++) {
                    $feild = 'file' . $i;
                    //如果有这张图片
                    if ($request->$feild) {
                        $extension = $request->$feild->extension();
                        if (!in_array($extension, ['png', 'jpg', 'jpeg', 'bmp', 'tiff', 'gif'])) {
                            DB::rollBack();

                            return ['status' => 2, 'message' => '图片格式不正确'];
                        }
                        //上传图片
                        $path = self::OssUpdate($request->$feild);
                        $path = 'https://rans-7900.oss-cn-beijing.aliyuncs.com/' . $path;
                        $picInsert[] = [
                            'member_id' => $request->user_id,
                            'archive_id' => $post_id,
                            'path' => $path,
                        ];
                    }
                }
                //数据库添加数据
                $pic_insert = DB::table('tbl_picture')->insert($picInsert);
                if (!$pic_insert) {
                    DB::rollBack();

                    return ['status' => 2, 'message' => '图片上传失败'];
                }
            } else {//上传的视频
                $extension = $request->file1->extension();
                if (!in_array($extension, ['rm', 'rmvb', 'mov', '3gp', 'mp4', 'flash', 'avi', 'wma', 'mid', 'm4v'])) {
                    DB::rollBack();

                    return ['status' => 2, 'message' => '视频格式不正确'];
                }
                $filename = self::OssVideo($request->file1);
                if ($filename['status'] !== 1) {
                    DB::rollBack();

                    return $filename;
                }
                $filename = $filename['message'];
                $video_path = Storage::disk('public')->put('/', $request->file1);
                //截取视频第一帧
                $pic_path = storage_path('app/public/test.png');
                $str = "ffmpeg -i " . storage_path('app/public/' . $video_path) . " -y -f mjpeg -ss 0 -t 1 " . $pic_path;
                system($str);//调用命令行
                //将截图上传到oss
                $pic = fopen($pic_path, 'r');
                Storage::disk('oss')->put($filename . '.png', $pic);
                //删除视频和截图
                Storage::disk('public')->delete($video_path);
                Storage::disk('public')->delete('test.png');
                $video_f = 'https://rans-7900.oss-cn-beijing.aliyuncs.com/' . $filename . '.png';
                //修改文章的video
                $video = DB::table('tbl_archive')->where('archive_id', $post_id)->update([
                    'video_f' => $video_f,
                    'video' => 'https://rans-7900.oss-cn-beijing.aliyuncs.com/' . $filename
                ]);
                if (!$video) {
                    DB::rollBack();

                    return ['status' => 2, 'message' => '视频上传失败'];
                }
            }
        }
        DB::commit();

        return ['status' => 1, 'message' => '发帖成功'];
    }

    /**
     * 上传文件到oss
     * @param $file
     * @return mixed
     */
    static function OssUpdate($file)
    {
        return $file->store('/', 'oss');
    }


    /**
     * 帖子详情
     * @param $post_id
     * @return array
     */
    static function PostingDetail($post_id)
    {
        $return = [];
        //帖子详情
        $post = DB::table('tbl_archive')->select('pub_time', 'content', 'member_id', 'video', 'title', 'keshi', 'huati', 'is_hot', 'keshi_id', 'huati_id')->where('archive_id', $post_id)->first();
        $return['pub_time'] = $post->pub_time;
        $return['content'] = $post->content;
        $return['title'] = $post->title;
        $return['keshi'] = $post->keshi;
        $return['keshi_id'] = $post->keshi_id;
        $return['huati'] = $post->huati;
        $return['huati_id'] = $post->huati_id;
        $return['is_hot'] = $post->is_hot;
        $return['archive_id'] = $post_id;
        //用户
        $user = DB::table('t_member')->select('name', 'c_avatar')->where('c_id', $post->member_id)->first();
        $return['name'] = $user->name;
        $return['c_avatar'] = $user->c_avatar;
        //回复数
        $ping_num = DB::table('tbl_article_comment')->where('article_id', $post_id)->count();
        $return['ping_num'] = $ping_num;
        //文件
        if ($post->video) {
            $return['video'] = $post->video;
        } else {
            $pics = DB::table('tbl_picture')->select('path', 'picture_id')->where('archive_id', $post_id)->get();
            foreach ($pics as $pic) {
                $return['pic'][] = [
                    'pic_id' => $pic->picture_id,
                    'path' => $pic->path
                ];
            }
        }

        return $return;
    }


    /**
     * 回帖
     * @param $archive_id
     * @param $user_id
     * @param $content
     * @return array
     */
    static function Huitie($archive_id, $user_id, $content, $view_time)
    {
        DB::beginTransaction();

        //添加behavior_log
        $behavior_log = BehaviorLog::UserBehaviorLog($user_id, 'comment_times');
        if ($behavior_log['status'] !== 1) {
            DB::rollBack();

            return $behavior_log;
        }

        //回帖
        $insert = DB::table('tbl_article_comment')->insertGetId([
            'create_time' => $view_time ? $view_time : date('Y-m-d H:i:s', time()),
            'article_id' => $archive_id,
            'member_id' => $user_id,
            'content' => $content,
            'good_num' => 0
        ]);

        if (!$insert) {
            DB::rollBack();

            return ['status' => 2, 'message' => '回复失败'];
        }
        //评论数+1
        $article = DB::table('tbl_archive')->where('archive_id', $archive_id)->lockForUpdate()->first();
        $result = DB::table('tbl_archive')->where('archive_id', $article->archive_id)->increment('ping_num');
        if (!$result) {
            DB::rollBack();

            return ['status' => 2, 'message' => '回复失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '回复成功'];
    }


    /**
     * 回复楼层的回复
     * @param $request
     * @return array
     */
    static function PostingRereply($request)
    {
        DB::beginTransaction();
        //添加behavior_log
        $behavior_log = BehaviorLog::UserBehaviorLog($request->user_id, 'comment_times');
        if ($behavior_log['status'] !== 1) {
            DB::rollBack();

            return $behavior_log;
        }
        //回复
        //用户名
        $reuser = DB::table('tbl_archive_reply')->select('user_id', 'user_name', 'lou_id')->find($request->reply_id);
        $reuser_name = $reuser->user_name;
        $reuser_id = $reuser->user_id;
        $actchive_id = $reuser->lou_id;
        $user_name = DB::table('t_member')->where('c_id', $request->user_id)->value('name');
        //保存
        $reply = DB::table('tbl_archive_reply')->insertGetId([
            'user_id' => $request->user_id,
            'user_name' => $user_name,
            'reuser_id' => $reuser_id,
            'reuser_name' => $reuser_name,
            'lou_id' => $actchive_id,
            'content' => $request->contents,
            'create_time' => $request->view_time ? $request->view_time : date('Y-m-d H:i:s', time())
        ]);

        if (!$reply) {
            DB::rollBack();

            return ['status' => 2, 'message' => '回复失败'];
        }

        //评论数+1
        $art_id = DB::table('tbl_article_comment')->where('id', $actchive_id)->value('article_id');
        //评论数+1
        $article = DB::table('tbl_archive')->where('archive_id', $art_id)->lockForUpdate()->first();
        $result = DB::table('tbl_archive')->where('archive_id', $article->archive_id)->increment('ping_num');
        if (!$result) {
            DB::rollBack();

            return ['status' => 2, 'message' => '回复失败'];
        }

        DB::commit();
        return ['status' => 1, 'message' => '回复成功'];
    }

    /**回复楼层
     * @param $request
     * @return array
     */
    static function PostingRereplys($request)
    {
        DB::beginTransaction();
        //添加behavior_log
        $behavior_log = BehaviorLog::UserBehaviorLog($request->user_id, 'comment_times');
        if ($behavior_log['status'] !== 1) {
            DB::rollBack();

            return $behavior_log;
        }
        //回复
        //保存
        $reply = DB::table('tbl_archive_reply')->insertGetId([
            'user_id' => $request->user_id,
            'user_name' => DB::table('t_member')->where('c_id', $request->user_id)->value('name'),
            'lou_id' => $request->actchive_id,
            'content' => $request->contents,
            'create_time' => $request->view_time ? $request->view_time : date('Y-m-d H:i:s', time())
        ]);

        if (!$reply) {
            DB::rollBack();
            return ['status' => 2, 'message' => '回复失败'];
        }

        //评论数+1
        $art_id = DB::table('tbl_article_comment')->where('id', $request->actchive_id)->value('article_id');
        //评论数+1
        $article = DB::table('tbl_archive')->where('archive_id', $art_id)->lockForUpdate()->first();
        $result = DB::table('tbl_archive')->where('archive_id', $article->archive_id)->increment('ping_num');
        if (!$result) {
            DB::rollBack();

            return ['status' => 2, 'message' => '回复失败'];
        }

        DB::commit();
        return ['status' => 1, 'message' => '回复成功'];
    }


    /**
     * 删除楼层回复
     * @param $reply_id
     * @return array
     * @throws \Exception
     */
    static function DelPostingReply($reply_id)
    {
        DB::beginTransaction();

        $lou_id = DB::table('tbl_archive_reply')->where('id', $reply_id)->value('lou_id');
        //评论数-1
        $art_id = DB::table('tbl_article_comment')->where('id', $lou_id)->value('article_id');
        $article = DB::table('tbl_archive')->where('archive_id', $art_id)->lockForUpdate()->first();
        $result = DB::table('tbl_archive')->where('archive_id', $article->archive_id)->decrement('ping_num');
        if (!$result) {
            DB::rollBack();

            return ['status' => 2, 'message' => '删除失败'];
        }

        //删除回复
        if (!DB::table('tbl_archive_reply')->where('id', $reply_id)->delete()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '删除失败'];
        }
        DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 删除楼层
     * @param $lou_id
     * @return array
     * @throws \Exception
     */
    static function DelReply($lou_id)
    {
        DB::beginTransaction();
        //应减少的评论数
        $num = DB::table('tbl_archive_reply')->where('lou_id', $lou_id)->count() + 1;
        $art_id = DB::table('tbl_article_comment')->where('id', $lou_id)->value('article_id');
        $article = DB::table('tbl_archive')->where('archive_id', $art_id)->lockForUpdate()->first();
        $result = DB::table('tbl_archive')->where('archive_id', $article->archive_id)->decrement('ping_num', $num);
        if (!$result) {
            DB::rollBack();

            return ['status' => 2, 'message' => '删除失败'];
        }
        //删除
        if (!DB::table('tbl_article_comment')->where('id', $lou_id)->delete()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '删除失败'];
        }
        DB::commit();

        return ['status' => 1, 'message' => '删除成功'];
    }

    /**
     * 修改文章
     * @param $request
     * @param $article_id
     * @return array
     * @throws \Exception
     */
    static function PostEdit($request, $article_id)
    {
        $art_id = $request->art_id;
        $user_id = DB::table('tbl_archive')->where('archive_id', $art_id)->value('member_id');
        DB::beginTransaction();
        //修改内容文章
        DB::table('tbl_archive')->where('archive_id', $art_id)->update(
            [
                'title' => $request->title,
                'keshi_id' => $request->keshi_id,
                'keshi' => DB::table('tbl_keshi')->where('id', $request->keshi_id)->value('title'),
                'huati_id' => $request->huati_id,
                'huati' => DB::table('tbl_huati')->where('id', $request->huati_id)->value('title'),
                'content' => strip_tags($request->contents),
            ]
        );

        //修改文件
        //上传视频或者图片
        $picInsert = [];
        if ($request->has('pic0') || $request->has('pic1') || $request->has('pic2') || $request->has('pic3') || $request->has('pic4') || $request->has('pic5') || $request->has('pic6') || $request->has('pic7') || $request->has('pic8')) {
            for ($i = 0; $i < 9; $i++) {
                $filename = 'pic' . $i;
                if ($request->has($filename)) {
                    $extension = $request->$filename->extension();
                    if (!in_array($extension, ['png', 'jpg', 'jpeg', 'bmp', 'tiff', 'gif'])) {
                        DB::rollBack();

                        return ['status' => 2, 'message' => '图片格式不正确'];
                    }
                    //上传图片
                    $path = self::OssUpdate($request->$filename);
                    $path = 'https://rans-7900.oss-cn-beijing.aliyuncs.com/' . $path;
                    $picInsert[] = [
                        'member_id' => $user_id,
                        'archive_id' => $art_id,
                        'path' => $path,
                    ];
                }
            }
            //数据库添加数据
            $pic_insert = DB::table('tbl_picture')->insert($picInsert);
            if (!$pic_insert) {
                DB::rollBack();

                return ['status' => 2, 'message' => '图片上传失败'];
            }
            if (DB::table('tbl_picture')->where('archive_id', $art_id)->count() > 9) {
                DB::rollBack();

                return ['status' => 2, 'message' => '图片上传失败'];
            }
        }


        if ($request->has('video')) {//上传的视频
            $extension = $request->video->extension();
            if (!in_array($extension, ['rm', 'rmvb', 'mov', '3gp', 'mp4', 'flash', 'avi', 'wma', 'mid'])) {
                DB::rollBack();

                return ['status' => 2, 'message' => '视频格式不正确'];
            }
            $filename = self::OssVideo($request->video);
            if ($filename['status'] !== 1) {
                DB::rollBack();

                return $filename;
            }
            $filename = $filename['message'];
            $video_path = Storage::disk('public')->put('/', $request->video);
            //截取视频第一帧
            $pic_path = storage_path('app/public/test.png');
            $str = "ffmpeg -i " . storage_path('app/public/' . $video_path) . " -y -f mjpeg -ss 0 -t 1 " . $pic_path;
            system($str);//调用命令行
            //将截图上传到oss
            $pic = fopen($pic_path, 'r');
            Storage::disk('oss')->put($filename . '.png', $pic);
            //删除视频和截图
            Storage::disk('public')->delete($video_path);
            Storage::disk('public')->delete('test.png');
            $video_f = 'https://rans-7900.oss-cn-beijing.aliyuncs.com/' . $filename . '.png';
            //修改文章的video
            $video = DB::table('tbl_archive')->where('archive_id', $art_id)->update([
                'video_f' => $video_f,
                'video' => 'https://rans-7900.oss-cn-beijing.aliyuncs.com/' . $filename
            ]);
            if (!$video) {
                DB::rollBack();

                return ['status' => 2, 'message' => '视频上传失败'];
            }
        }
        DB::commit();

        return ['status' => 1, 'message' => '修改成功'];
    }

    /**
     * 分片上传视频
     * @param $uploadFile
     * @return array
     */
    static function OssVideo($uploadFile)
    {
        $bucket = env('OSS_BUCKET');
        $pre = md5(basename($uploadFile) . time());
        $extension = $uploadFile->extension();
        $object = $pre . '.' . $extension;
        /**
         *  步骤1：初始化一个分片上传事件，获取uploadId。
         */
        try {
            $ossClient = new OssClient(env('OSS_ACCESSID'), env('OSS_ACCESSKEY'), env('OSS_ENDPOINT'));
            //返回uploadId，它是分片上传事件的唯一标识，您可以根据这个ID来发起相关的操作，如取消分片上传、查询分片上传等。
            $uploadId = $ossClient->initiateMultipartUpload($bucket, $object);
        } catch (OssException $e) {
            return ['status' => 2, 'message' => '视频上传失败'];
        }
        /*
         * 步骤2：上传分片。
         */
        $partSize = 10 * 1024 * 1024;
        $uploadFileSize = filesize($uploadFile);
        $pieces = $ossClient->generateMultiuploadParts($uploadFileSize, $partSize);
        $responseUploadPart = array();
        $uploadPosition = 0;
        $isCheckMd5 = true;
        foreach ($pieces as $i => $piece) {
            $fromPos = $uploadPosition + (integer)$piece[$ossClient::OSS_SEEK_TO];
            $toPos = (integer)$piece[$ossClient::OSS_LENGTH] + $fromPos - 1;
            $upOptions = array(
                $ossClient::OSS_FILE_UPLOAD => $uploadFile,
                $ossClient::OSS_PART_NUM => ($i + 1),
                $ossClient::OSS_SEEK_TO => $fromPos,
                $ossClient::OSS_LENGTH => $toPos - $fromPos + 1,
                $ossClient::OSS_CHECK_MD5 => $isCheckMd5,
            );
            // MD5校验。
            if ($isCheckMd5) {
                $contentMd5 = OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos);
                $upOptions[$ossClient::OSS_CONTENT_MD5] = $contentMd5;
            }
            try {
                // 上传分片。
                $responseUploadPart[] = $ossClient->uploadPart($bucket, $object, $uploadId, $upOptions);
            } catch (OssException $e) {
                return ['status' => 2, 'message' => '视频分片上传失败'];
            }
        }
// $uploadParts是由每个分片的ETag和分片号（PartNumber）组成的数组。
        $uploadParts = array();
        foreach ($responseUploadPart as $i => $eTag) {
            $uploadParts[] = array(
                'PartNumber' => ($i + 1),
                'ETag' => $eTag,
            );
        }
        /**
         * 步骤3：完成上传。
         */
        try {
            // 在执行该操作时，需要提供所有有效的$uploadParts。OSS收到提交的$uploadParts后，会逐一验证每个分片的有效性。当所有的数据分片验证通过后，OSS将把这些分片组合成一个完整的文件。
            $ossClient->completeMultipartUpload($bucket, $object, $uploadId, $uploadParts);
        } catch (OssException $e) {
            return ['status' => 2, 'message' => '视频上传完成失败'];
        }
        return ['status' => 1, 'message' => $object];
    }


    /**
     * 删除帖子
     * @param $ids
     * @return array
     * @throws \Exception
     */
    static function PostingDelect($ids, $message = '')
    {
        DB::beginTransaction();

        //删除
        if (is_array($ids)) {
            if (!DB::table('tbl_archive')->whereIn('archive_id', array_filter($ids))->update(['is_view' => Postings::VIEW_NO, 'view_time' => null])) {
                DB::rollBack();

                return ['status' => 2, 'message' => '删除失败'];
            }
            $times = count($ids);
            $user_ids = Postings::whereIn('archive_id', array_filter($ids))->pluck('member_id')->toArray();
        } else {
            if (!DB::table('tbl_archive')->where('archive_id', $ids)->update(['is_view' => Postings::VIEW_NO, 'view_time' => null])) {
                DB::rollBack();

                return ['status' => 2, 'message' => '删除失败'];
            }
            $times = 1;
            $user_ids = [Postings::where('archive_id', $ids)->value('member_id')];
        }

        //添加behavior_log
        $behavior_log = BehaviorLog::UserBehaviorLog(47, 'del_post_times', $times);
        if ($behavior_log['status'] !== 1) {
            DB::rollBack();

            return $behavior_log;
        }

        //系统消息
        if ($message) {
            foreach ($user_ids as $user_id) {
                //添加系统消息
                $re = Curl::PushOneMessage($user_id, $message);
                if ($re['status'] !== 1) {
                    DB::rollBack();

                    return $re;
                }
            }
        }

        DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * @param $id
     * @param $time
     * @return array
     */
    static function PostingJj($id, $time)
    {
        if (intval($time) === 1) {
            if (!DB::table('tbl_archive')->where('archive_id', $id)->update([
                'is_hot' => Postings::HOT_YES,
                'hot_time' => date('Y-m-d H:i:s', time())
            ])) {
                return ['status' => 2, 'message' => '失败'];
            }
        } else {
            if (!DB::table('tbl_archive')->where('archive_id', $id)->update([
                'hot_time' => $time
            ])) {
                return ['status' => 2, 'message' => '失败'];
            }
        }

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * @param $request
     * @return array
     */
    static function PostTongji($request)
    {
        //发帖数
        $postings = DB::table('tbl_archive');
        $ping = DB::table('tbl_article_comment');
        $reply = DB::table('tbl_archive_reply');
        $user_name = '全部用户';
        if ($request->user_name) {
            $user_name = $request->user_name;
            $user_id = User::where('name', $request->user_name)->value('c_id');
            $postings->where('member_id', $user_id);
            $ping->where('member_id', $user_id);
            $reply->where('user_id', $user_id);
        }
        if ($request->time) {
            $time = date2Date($request->time);
            $postings->where('pub_time', '>', $time[0])->where('pub_time', '<', $time[1]);
            $ping->where('create_time', '>', $time[0])->where('create_time', '<', $time[1]);
            $reply->where('create_time', '>', $time[0])->where('create_time', '<', $time[1]);
        }
        $postings = $postings->count();
        $pings = $ping->count()+$reply->count();
        return compact('user_name', 'postings', 'pings');
    }

}
